/*
 * Copyright 2015 MingxingWang

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.spark.graphx.graphdb

import org.apache.spark.graphx.VertexId

import scala.reflect.ClassTag
import org.apache.spark.graphx.impl.VertexPartitionBase

class MyVertexPartition[VD: ClassTag](val from: VertexPartitionBase[VD]) extends Serializable {
  val index = from.index
  val values = from.values
  val mask = from.mask


  def delete(vid: VertexId): Boolean = {
    val pos = index.getPos(vid)
    if (pos >= 0 && mask.get(pos)) {
      mask.unset(pos)
      true
    } else {
      false
    }
  }

  def update(vid: VertexId, vd: VD): Boolean = {
    val pos = index.getPos(vid)
    if (pos >= 0 && mask.get(pos)) {
      values(pos) = vd
      true
    } else {
      false
    }
  }
}

object MyVertexPartition {
  implicit def vp2myvp[VD: ClassTag](from: VertexPartitionBase[VD]) = new MyVertexPartition(from)
}
